//
//  ContainerManagerViewController.m
//  收纳
//
//  Created by  on 2022/10/27.
//

#import "ContainerManagerViewController.h"
#import "MineCell.h"
#import "AddViewController.h"

@interface ContainerManagerViewController ()
@property (strong, nonatomic) IBOutlet UIButton *btn;

@end

@implementation ContainerManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
- (void)initUI{
    self.title = LS(@"容器管理");
    self.tableViewStyle  = UITableViewStyleGrouped;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerNib:[MineCell nib] forCellReuseIdentifier:[MineCell ID]];
    self.tableView.c_left.equalTo(self.view).coffset(20);
    self.tableView.c_right.equalTo(self.view).coffset(-20);
    self.tableView.c_top.equalTo(self.view).coffset(0);
    self.tableView.c_bottom.equalTo(self.view).coffset(0);
    [self.view bringSubviewToFront:self.btn];
}
- (void)loadData{
    [GoodsTool find:YES complete:^(NSMutableArray<Goods *> * _Nonnull result) {
        self.array = [NSMutableArray arrayWithArray:result];
        [self.tableView reloadData];
    }];
}
- (IBAction)actionForBtn:(UIButton *)sender {
    AddViewController *vc = [AddViewController new];
    vc.model = [Goods new];
    vc.model.isContainer = NO;
    vc.model.stamp = [[NSDate currentTimeStamp:@"yyyy-MM-dd HH:mm:ss"] integerValue];
    [self push:vc :YES];
}
- (void)addNotify{
    [NotifyTool add:self :@selector(loadData) :notifyGoodsUpdate];
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.array.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MineCell *cell = [tableView dequeueReusableCellWithIdentifier:[MineCell ID]];
    Goods *model = self.array[indexPath.section];
    cell.title.text = model.name;
    cell.type = MineCellTypeDelete;
    cell.block = ^(BOOL on) {
        [GoodsTool delete:model complete:^(BOOL success) {
        }];
    };
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section > 0) { return nil;}
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
@end

